package com.yyw.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyw.common.utils.PageUtils;
import com.yyw.common.utils.Query;
import com.yyw.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.yyw.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.yyw.gulimall.product.service.AttrAttrgroupRelationService;
import com.yyw.gulimall.product.vo.AttrGroupRelationVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveByAttrAndAttrGroup(List<AttrGroupRelationVo> vos) {
        if (!vos.isEmpty()) {
            List<AttrAttrgroupRelationEntity> list = vos.stream().map(e -> {
                AttrAttrgroupRelationEntity entity = AttrAttrgroupRelationEntity.builder()
                        .attrGroupId(e.getAttrGroupId())
                        .attrId(e.getAttrId())
                        .attrSort(0)
                        .build();
                return entity;
            }).collect(Collectors.toList());
            this.saveBatch(list);
        }
    }

}